﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ZenInterface
{
    enum hardwareState
    {
        notConnected = 0,
        ready = 1,
        error = -1,
        dataIn = 2,
        dataOut = 3
    }

    public partial class zenHardware : Form
    {
        int state;
        zenStatusClass zenStatus;
        String outBuffer;
        bool enableRollingAverage;
        bool connected;
        Properties.Settings settings;

        public zenHardware(Properties.Settings inSettings, zenStatusClass inZenStatus)
        {
            InitializeComponent();

            settings = inSettings;
            outBuffer = String.Empty;
            zenStatus = inZenStatus;

            state = (int)hardwareState.notConnected;
            maskedTextBoxRollingAverage.Text = hScrollBarRollingAverage.Value.ToString();
            maskedTextBoxDifferentialFilter.Text = hScrollBarDifferentialFilter.Value.ToString();
            stateTranslate(state);

            checkBoxRollingAverageEnable.DataBindings.Add((new System.Windows.Forms.Binding("Checked", settings, "userHardwareEnableRollingAverage", true, DataSourceUpdateMode.OnPropertyChanged)));
            checkBoxDifferentialFilter.DataBindings.Add((new System.Windows.Forms.Binding("Checked", settings, "userHardwareEnableDifferentialFilter", true, DataSourceUpdateMode.OnPropertyChanged)));

            //settings.userHardwareState = Properties.Settings.Default.userHardwareState;
            //settings.userHardwareState = (int)hardwareState.notConnected;
            //stateTranslate(settings.userHardwareState);
        }


        public void setupVariables(bool inEnableRollingAverage)
        {
            enableRollingAverage = inEnableRollingAverage;
        }

        #region parent communication
        //update display form display and return the state of the form
        public int update()
        {
            radioButton0.Checked = zenStatus.button[0];
            radioButton1.Checked = zenStatus.button[1];
            radioButton2.Checked = zenStatus.button[2];
            radioButton3.Checked = zenStatus.button[3];

            radioButtonLED0.Checked = zenStatus.LED[0];
            radioButtonLED1.Checked = zenStatus.LED[1];
            radioButtonLED2.Checked = zenStatus.LED[2];
            radioButtonLED3.Checked = zenStatus.LED[3];

            labelVersion.Text = zenStatus.version;
            return state;
        }

        //get the rolling average constant value
        public int getRollingAverageValue()
        {

            if (checkBoxRollingAverageEnable.Checked)
            {
                return hScrollBarRollingAverage.Value;
            }
            return -1;            
        }
        //get the differential average constant value
        public int getDifferentialFilterValue()
        {
            if (checkBoxDifferentialFilter.Checked)
            {
                return hScrollBarDifferentialFilter.Value;
            }

            return -1;
        }
        //sets the status based on if the hardware device is connected or not
        public void setStatus(bool inConnected)
        {
            connected = inConnected;

            if (connected)
            {
                state = 1;
            }

            else
            {
                state = 0;
            }

            stateTranslate(state);
        }
        //translates the enumerated state values
        public String stateTranslate(int inState)
        {
            String outputBuffer = String.Empty;

            switch (inState)
            {
                case ((int)hardwareState.notConnected):
                    outputBuffer = Properties.Resources.stringNotConnected;
                    break;

                case ((int)hardwareState.ready):
                    outputBuffer = Properties.Resources.stringReady;
                    break;

                case ((int)hardwareState.error):
                    outputBuffer = Properties.Resources.stringError;
                    break;

                case ((int)hardwareState.dataOut):
                    outputBuffer = Properties.Resources.stringDataToHardware;
                    break;

                case ((int)hardwareState.dataIn):
                    outputBuffer = Properties.Resources.stringHardwaretoData;
                    break;

                default:
                    outputBuffer = Properties.Resources.stringUnknown;
                    break;
            }

            toolStripStatusLabel.Text = outputBuffer;

            return outputBuffer;
        }
        #endregion

        #region variable manipulation
        //do some magic with the scroll pars to update the text box when user changes it
        private void hScrollBarDifferentialFilter_ValueChanged(object sender, EventArgs e)
        {
            maskedTextBoxDifferentialFilter.Text = hScrollBarDifferentialFilter.Value.ToString();
        }
        private void hScrollBarRollingAverage_ValueChanged(object sender, EventArgs e)
        {
            maskedTextBoxRollingAverage.Text = hScrollBarRollingAverage.Value.ToString();
        }
//TODO add the change to the scroll when user changes the masked text box... without an infinit loop
        //this doesn't seem to do anything, but I need to find a way to capture the close window command and just hide my form
        private void zenHardware_KeyPress(object sender, KeyPressEventArgs e)
        {
            switch (e.KeyChar)
            {
                case ((char)Keys.Escape):
                    this.Hide();
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region buttons
        //save the configuration in the user configuration settings
        private void buttonSaveConfiguration_Click(object sender, EventArgs e)
        {

            settings.Save();
            //Properties.Settings.Default.userHardwareDifferentialFilter = maskedTextBoxDifferentialFilter.Text;
            
        }
        //write data to the hardware
        private void buttonWrite_Click(object sender, EventArgs e)
        {
            state = 2;
            stateTranslate(state);
        }
        //read data from the hardwar
        private void buttonRead_Click(object sender, EventArgs e)
        {
            state = 3;
            stateTranslate(state);
        }
        #endregion

        private void buttonExit_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void zenHardware_FormClosing(object sender, FormClosingEventArgs e)
        {
            

            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                this.Hide();
            }
            
        }

    }
}
